package com.sm.imlibrary.roondao;

import androidx.room.Room;
import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.scorpio.baselibrary.utils.Base64Utils;
import com.scorpio.baselibrary.utils.ConvertUtils;
import com.scorpio.baselibrary.utils.JSONUtil;
import com.sm.imlibrary.roondao.entity.DbChatImage;
import com.sm.imlibrary.roondao.entity.DbChatMessage;
import com.sm.imlibrary.roondao.entity.DbChatText;
import com.sm.imlibrary.roondao.entity.DbUserInfo;
import com.sm.imlibrary.model.SIMElemType;
import com.sm.imlibrary.model.http.MessageBean;
import com.sm.imlibrary.model.http.UserBean;

import java.util.ArrayList;
import java.util.List;

/**
 * created by zhud on 2019/7/31
 */
public class RoomDaoApi {
    private AppDatabase database;
    private static RoomDaoApi mInstance;

    public static RoomDaoApi getInstance() {
        if (mInstance == null) {
            synchronized (RoomDaoApi.class) {
                if (mInstance == null) {
                    mInstance = new RoomDaoApi();
                }
            }
        }
        return mInstance;
    }


    public AppDatabase getDatabase() {
        return database;
    }

    /**
     * 初始化数据库对象
     */
    public void initDatabase(Context context, String identifying) {
        database = Room.databaseBuilder(context, AppDatabase.class, identifying).build();
    }

    public void saveChatMessage(List<MessageBean> messageBeans) {
        List<DbChatMessage> chatMessages = new ArrayList<>();
        List<DbChatText> chatTexts = new ArrayList<>();
        List<DbChatImage> chatImages = new ArrayList<>();
        Gson gson = new Gson();
        for (MessageBean messageBean : messageBeans) {
            DbChatMessage chatMessage = ConvertUtils.sourceToTarget(messageBean, DbChatMessage.class);
            JSONObject jsonContent = JSONUtil.toJSONObject(Base64Utils.decode(messageBean.message));
            if (SIMElemType.Text.name().equals(messageBean.messageType)) {
                DbChatText chatText = gson.fromJson(jsonContent.toJSONString(), DbChatText.class);
                chatText.id = jsonContent.getLong("id");
                chatText.content = jsonContent.getString("content");
                chatTexts.add(chatText);
                chatMessage.messageTextId = chatText.id;
            } else if (SIMElemType.Image.name().equals(messageBean.messageType)) {
                DbChatImage chatImage = gson.fromJson(jsonContent.toJSONString(), DbChatImage.class);
                chatImages.add(chatImage);
            }
            chatMessages.add(chatMessage);
        }
        if (chatMessages.size() > 0) {
            RoomDaoApi.getInstance().getDatabase().chatMessageDao().insertAll(chatMessages);
        }
        if (chatTexts.size() > 0) {
            RoomDaoApi.getInstance().getDatabase().chatTextDao().insertAll(chatTexts);
        }
        if (chatImages.size() > 0) {
            RoomDaoApi.getInstance().getDatabase().chatImageDao().insertAll(chatImages);
        }
    }

    public void saveUserInfo(List<UserBean> userBeans) {
        List<DbUserInfo> userInfos = new ArrayList<>();
        for (UserBean userBean : userBeans) {
            DbUserInfo userInfo = ConvertUtils.sourceToTarget(userBean, DbUserInfo.class);
            userInfos.add(userInfo);
        }
        if (userInfos.size() > 0)
            RoomDaoApi.getInstance().getDatabase().userInfoDao().insertAll(userInfos);
    }
}
